﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace AlbumsExport
{
    class AlbumsExport
    {
        static void Main()
        {
            const string SourceXML = @"..\..\..\catalog.xml";
            const string DestinationXML = @"..\..\album.xml";

            XmlReader reader = XmlReader.Create(SourceXML);
            using (reader)
            {
                XmlWriter writer = XmlWriter.Create(DestinationXML);
                using (writer)
                {
                    writer.WriteStartDocument();
                    writer.WriteStartElement("albums");
                    bool openAlbumTag = true;
                    while (reader.Read())
                    {
                        if (reader.Name == "album")
                        {
                            if (openAlbumTag == true)
                            {
                                writer.WriteStartElement("album");
                            }
                            else
                            {
                                writer.WriteEndElement();
                            }
                            openAlbumTag = !openAlbumTag;
                        }
                        if (reader.NodeType == XmlNodeType.Element && reader.Name == "name")
                        {
                            writer.WriteElementString("name", (reader.ReadElementString()));
                        }
                        if (reader.NodeType == XmlNodeType.Element && reader.Name == "artist")
                        {
                            writer.WriteElementString("artist", (reader.ReadElementString()));
                        }
                    }
                    writer.WriteEndElement();
                    writer.WriteEndDocument();
                }
            }
        }
    }
}
